package ar.com.psicosoft.persistence.psicosoft;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ar.com.psicosoft.exception.PsicosoftException;
import ar.com.psicosoft.model.psicosoft.NivelEstudio;

@Repository(value = "nivelEstudioDao")
public class NivelEstudioDaoImpl implements NivelEstudioDao, Serializable {

	private static final long serialVersionUID = 1L;
	@PersistenceContext(unitName = "psicosoftPU")
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	@Override
	public NivelEstudio buscarNivelEstudioPorId(Integer idNivelEstudio)
			throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("NivelEstudio.buscarPorId");
		query.setParameter("idNivelEstudio", idNivelEstudio);
		try {
			List<NivelEstudio> resultList = query.getResultList();
			if (!resultList.isEmpty()) {
				// ignores multiple results
				return resultList.get(0);
			} else {
				return null;
			}
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar nivelEstudio en la Base de Datos");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public NivelEstudio buscarNivelEstudioPorNombreNivelEstudio(
			String nombreNivelEstudio) throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("NivelEstudio.buscarPorNombreNivelEstudio");
		query.setParameter("nombreNivelEstudio", nombreNivelEstudio);
		try {
			List<NivelEstudio> resultList = query.getResultList();
			if (!resultList.isEmpty()) {
				// ignores multiple results
				return resultList.get(0);
			} else {
				return null;
			}
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar nivelEstudio en la Base de Datos");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<NivelEstudio> buscarNivelesEstudio() throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("NivelEstudio.buscarNivelesEstudio");
		try {
			List<NivelEstudio> resultList = query.getResultList();
			return resultList;
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar nivelEstudios en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void guardarNivelEstudio(NivelEstudio nivelEstudio)
			throws PsicosoftException {
		try {
			this.entityManager.persist(nivelEstudio);
			this.entityManager.flush();
			this.entityManager.refresh(nivelEstudio);
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al insertar nivelEstudio en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void modificarNivelEstudio(NivelEstudio nivelEstudio)
			throws PsicosoftException {
		try {
			this.entityManager.merge(nivelEstudio);
			this.entityManager.flush();
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al modificar nivelEstudio en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void eliminarNivelEstudio(NivelEstudio nivelEstudio)
			throws PsicosoftException {
		try {
			this.entityManager.remove(this.entityManager.merge(nivelEstudio));
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al eliminar nivelEstudio en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void bajaNivelEstudio(NivelEstudio nivelEstudio)
			throws PsicosoftException {
		try {
			this.entityManager.merge(nivelEstudio);
			this.entityManager.flush();
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al modificar nivelEstudio en la Base de Datos");
		}
	}

}